//
//  ZHHospitalMainViewController.m
//  Doctor
//
//  Created by liuzhishan on 15/12/15.
//  Copyright © 2015年 shanshan. All rights reserved.
//

#import "ZHHospitalMainViewController.h"
#import "ZHClinicModel.h"
#import "ZHClinicStatisticsView.h"
#import "ZHRightView.h"
#import "ZHBtnChooseDele.h"
#import "ZHHospitalModel.h"
#import "ZHClinicIncomeViewController.h"
#import "ZHClinicPatientViewController.h"
#import "ZHClinicStatisticsViewController.h"
#import "ZHClinicView.h"
#import "ZHZHGetLanguageTool.h"
#import "DataTool.h"
#import "ZHArrayTool.h"


@interface ZHHospitalMainViewController ()<ZHBtnChooseDele>

@property (nonatomic,retain) ZHClinicStatisticsView *leftView;
@property (nonatomic,retain) ZHRightView *rightUpView;
@property (nonatomic,retain) ZHRightView *rightDownView;

@property (nonatomic,retain) UIImageView *imageLogo;

@property (nonatomic,retain) ZHClinicModel *model;


@property (nonatomic,retain) UILabel *naviLeftLable;
@property (nonatomic,retain) UILabel *naviRightLable;

@property (nonatomic,retain) NSArray *controllerNameArray;

@property (nonatomic,retain) NSString *leftUrl;
@property (nonatomic,retain) NSString *rightUpUrl;
@property (nonatomic,retain) NSString *rightDownUrl;


@property (nonatomic,retain) NSMutableArray *leftArray;

@property (nonatomic,retain) NSMutableArray *rightUpArray;

@property (nonatomic,retain) NSMutableArray *rightDownArray;

@property (nonatomic,retain) NSArray *urlArray;


@end

@implementation ZHHospitalMainViewController

 

-(void)writeVCIndexToFile:(NSInteger) index
{
    
    [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInteger:index] forKey:@"VCIndex"];
    
    NSNumber *nub = [NSNumber numberWithInteger:self.hosModel.shopIdentity];
    [[NSUserDefaults standardUserDefaults] setValue:nub forKey:@"ClinicID"];
    
}

#pragma  mark 跳转到下个页面
-(void)pushMyChooseController:(NSInteger)index
{
    
    [self writeVCIndexToFile:index];
    
 
    NSString *className = self.controllerNameArray[index - 1];
    
    Class class = NSClassFromString(className);
    
    ZHClinicBaseViewController *baseCon = [[class alloc]init];
    baseCon.clinicURL = self.urlArray[index -1];
   
    
    [self.navigationController pushViewController:baseCon animated:YES];
    
    
    
        
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
 
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setMyUrlAndModel];
    
    
     [self setMycontrollerArray];
     [self addMyBgImageView];
    
    [self addMyChildViews];
    [self getDataWithURl];
    [self addMyBaseNaviBackBtn];
    
}

-(void)setMyUrlAndModel
{
    
    self.model = [[ZHClinicModel alloc]init];
    
    
    if (!self.isAll) {
    
  
        self.leftUrl      = [NSString stringWithFormat:@"%@/%ld",CLINICDEFAULTSTATISTICSURL,self.hosModel.shopIdentity];
        self.rightUpUrl   = [NSString stringWithFormat:@"%@/%ld",CLINICDEFAULTINCOMEURL,self.hosModel.shopIdentity];
        self.rightDownUrl = [NSString stringWithFormat:@"%@/%ld",CLINICDEFAULTPATIENTERURL,self.hosModel.shopIdentity];
       
    }
    
    else
    {
        self.leftUrl      = [NSString stringWithFormat:@"%@%@/%@",CLINICDEFAULTSTATISTICSURL,[ZHGetOwerID getOwerID],@"all"];
        self.rightUpUrl   = [NSString stringWithFormat:@"%@%@/%@",CLINICDEFAULTINCOMEURL,[ZHGetOwerID getOwerID],@"all"];
        self.rightDownUrl = [NSString stringWithFormat:@"%@%@/%@",CLINICDEFAULTPATIENTERURL,[ZHGetOwerID getOwerID],@"all"];
        
    }
    
    
    self.urlArray = @[self.leftUrl,self.rightUpUrl,self.rightDownUrl];
    
    
}

-(void)setMycontrollerArray
{
    self.controllerNameArray = @[@"ZHClinicStatisticsViewController",@"ZHClinicIncomeViewController",@"ZHClinicPatientViewController"];
}

-(void)addMyBaseNaviBackBtn
{
    
    UIImageView *imageView = [[UIImageView alloc]init];
    [imageView setMyFramewithX:10 andY:34 andWidth:50 andHeight:20];
    imageView.image = [UIImage imageNamed:@"NaviBack"];
    
    imageView.userInteractionEnabled = YES;
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backDismiss)];
    
    [imageView addGestureRecognizer:tap];
    
        [self.view addSubview:imageView];
    
    
    
}

-(void)backDismiss
{
    [self.navigationController popViewControllerAnimated:YES];
}



-(void)addMyChildViews
{
    self.leftView = [[ZHClinicStatisticsView alloc]init];
   // self.leftView.backgroundColor =BGCOLOR;
    [self.leftView setMyFramewithX:14 andY:94 andWidth:410 andHeight:618];
    self.leftView.image.frame = self.leftView.bounds;
    [self.view addSubview:self.leftView];
    self.leftView.dele  =self;
    
    self.rightUpView = [[ZHRightView alloc]init];
    [self.rightUpView setMyFramewithX:431 andY:94 andWidth:586 andHeight:314];
//    self.rightUpView.backgroundColor = BGCOLOR;
    self.rightUpView.image.frame = self.rightUpView.bounds;
    [self.view addSubview:self.rightUpView];
    self.rightUpView.titleName = @"诊所收入";
    self.rightUpView.lableClinicUp.text = @"诊所收费";
    [self.rightUpView.lableClinicUp sizeToFit];
    self.rightUpView.lableClinicDown.text = @"客单价";
    [self.rightUpView.lableClinicDown sizeToFit];
    self.rightUpView.dele = self;
 
    self.rightDownView = [[ZHRightView alloc]init];
    [self.rightDownView setMyFramewithX:431 andY:404 andWidth:586 andHeight:314];
//    self.rightDownView.backgroundColor = BGCOLOR;
    self.rightDownView.lableClinicUp.text = @"初诊人数";
    self.rightDownView.lableClinicDown.text = @"复诊人数";
    
    [self.rightDownView.lableClinicUp sizeToFit];
    [self.rightDownView.lableClinicDown sizeToFit];
    
    
      self.rightDownView.image.frame = self.rightDownView.bounds;
    
    [self.view addSubview:self.rightDownView];
     self.rightDownView.titleName = @"患者数";
    self.rightDownView.dele = self;
    
    
}

#pragma mark 添加约束
-(void)addLayoutView
{
    
    ZHClinicView *view =  [[ZHClinicView alloc]init];
    [view setMyFramewithX:0 andY:20 andWidth:1024 andHeight:48];
    [self.view addSubview:view];
    [view setClinicName:self.hosModel.shopName andUSerName:self.hosModel.shopContacts   andTelephone:self.hosModel.shopTelephone];
    
}

-(void)addMyBgImageView
{
    self.naviTitleLable.text = @"";
    if (!self.isAll) {
         [self addLayoutView];
    }
    
    else
    {
        self.naviTitleLable.text = @"所有诊所信息";
       
    }
    self.imageBg.image = [UIImage imageNamed:@"background2"];
 
}

-(void)getDataWithURl
{
    
    [self getLeftViewData];
    [self getRightUpViewData];
    [self getRightDownViewData];
    
    
}

-(void)getLeftViewData
{
    [AFNData getAFNDataWithUrl:self.leftUrl success:^(id responseObject) {
  
        NSDictionary *dic = responseObject;
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:[dic objectForKey:@"payload"]];

        self.leftArray = array;
        [self setLeftData:array];
        
        
        
    } failure:^(NSError *error) {
        
        
    }];
    

}
-(void)setLeftData:(NSMutableArray *)array
{
     NSInteger appointmentNumber = 0;
     NSInteger appointNumber = 0;
     NSInteger patienterNumber = 0;
    
    for (int i = 0;  i < array.count; i++) {
        
        NSDictionary *dic = array[i];
        
        appointmentNumber += [[dic objectForKey:@"appointmentNumber"] integerValue];
        appointNumber += [[dic objectForKey:@"appointNumber"] integerValue];
        patienterNumber += [[dic objectForKey:@"patienterNumber"] integerValue];
    }
    
    self.model.appointmentNumber = [NSNumber numberWithInteger:appointmentNumber];
    self.model.appointNumber = [NSNumber numberWithInteger:appointNumber];
    self.model.patienterNumber = [NSNumber numberWithInteger:patienterNumber];

    self.leftView.clinicModel = self.model;
    
}

-(void)getRightUpViewData
{
    
    [AFNData getAFNDataWithUrl:self.rightUpUrl success:^(id responseObject) {
        
        NSDictionary *dic = responseObject;
        NSLog(@"%@",self.rightUpUrl);
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:[dic objectForKey:@"payload"]];
        self.rightUpArray = array;
        
        [self setrightUpData:array];
        
        
        
    } failure:^(NSError *error) {
        
        
    }];
    
}

-(void)setrightUpData:(NSMutableArray *)array
{
    
    NSMutableArray *arrayDayPrice = [NSMutableArray array];
    CGFloat allIncome = 0;
    
    
    for (int i = 0;  i < array.count; i++) {
        
        NSDictionary *dic = array[i];
        
        allIncome += [[dic objectForKey:@"allIncome"] floatValue];
        [arrayDayPrice addObject:[dic objectForKey:@"topPrice"]];
        
    }
    
    self.model.allIncome = [NSNumber numberWithInteger:allIncome];
    self.model.topPrice = [NSNumber numberWithFloat:[ZHArrayTool getTheMaxInDataArray:arrayDayPrice]];
    
     self.rightUpView.model = self.model;
    
    
}



-(void)getRightDownViewData
{
    [AFNData getAFNDataWithUrl:self.rightDownUrl success:^(id responseObject) {
        
        NSDictionary *dic = responseObject;
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:[dic objectForKey:@"payload"]];
        self.rightDownArray = array;
        
        [self setrightDownData:array];
        
        
        
    } failure:^(NSError *error) {
        
        
    }];

}


-(void)setrightDownData:(NSMutableArray *)array
{
  
    
    NSInteger firstVisit = 0;
    NSInteger secondVisit = 0;
   
    
    for (int i = 0;  i < array.count; i++) {
        
        NSDictionary *dic = array[i];
        
        firstVisit += [[dic objectForKey:@"appointmentNumber"] integerValue];
        secondVisit += [[dic objectForKey:@"appointNumber"] integerValue];
        
    }
    
    self.model.firstVisit = [NSNumber numberWithInteger:firstVisit];
    self.model.secondVisit = [NSNumber numberWithInteger:secondVisit];
    self.rightDownView.model = self.model;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
